package com.itheima.server.controller;

import com.itheima.common.core.domain.AjaxResult;
import com.itheima.common.core.domain.R;
import com.itheima.server.domain.Contract;
import com.itheima.server.dto.ChangeContractDto;
import com.itheima.server.dto.ContractPageDto;
import com.itheima.server.dto.PageDto;
import com.itheima.server.service.ContractService;
import com.itheima.server.vo.ContractDeatilsVo;
import com.itheima.server.vo.ContractVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController {
    @Autowired
    private ContractService contractService;

    /**
     * 获取合同列表
     * @param dto
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult getPageContractList(ContractPageDto dto){
        AjaxResult ajaxResult = contractService.getPageContractList(dto);
        return ajaxResult;
    }

    /**
     * 新增合同
     * @return
     */
    @PostMapping
    public R addContract(@RequestBody Contract contract) {
        boolean flag = contractService.addContract(contract);
        if (!flag){
            return R.fail("添加失败");
        }
        return R.ok();
    }




    /**
     * 商机转合同
     * @param id
     * @param changeContractDto
     * @return
     */
    @PutMapping("/changeContract/{id}")
    public AjaxResult changeContract(@PathVariable Integer id, @RequestBody ChangeContractDto changeContractDto){
        AjaxResult ajaxResult = contractService.changeContract(id,changeContractDto);
        return ajaxResult;

    }


    /**
     * 获取合同详情 根据id
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public AjaxResult getContractById(@PathVariable Integer id){
        Contract contract = contractService.getById(id);
        return AjaxResult.success(contract);
    }

    /**
     * 修改合同
     * @return
     */
    @PutMapping
    public AjaxResult updateContract(@RequestBody Contract contract){
        Boolean flag = contractService.updateContract(contract);
        if (!flag){
            return AjaxResult.error("修改失败");
        }
        return AjaxResult.success("修改成功");
    }


    /**
     * 获取合同详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AjaxResult getContractDetails(@PathVariable Integer id){

        ContractDeatilsVo contractDeatilsVo = contractService .getContractDetails(id);


        return AjaxResult.success(contractDeatilsVo);
    }





}
